package com.springcloud.command;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class ObserverCommandDemo extends HystrixObservableCommand<String> {

    private String name;

    public ObserverCommandDemo(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ObserverCommandDemo"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("ObserverCommandKey")));
        this.name = name;
    }

    @Override
    protected Observable<String> construct() {
        System.out.println("current thread："+Thread.currentThread().getName());

        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("action 1,name = " + name);
                subscriber.onNext("action 2,name = " + name);
                subscriber.onNext("action 3,name = " + name);
                subscriber.onNext("action 4,name = " + name);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io());
    }
}
